<?php

namespace common\services;

use common\models\Question;
use Yii;
use yii\data\Pagination;
use yii\helpers\Json;

class QuestionService extends  BaseService
{




    public static function create($formData){
        $model = new Question();
        $model->load($formData,'');
        if ($model->validate() && $model->save()) {
            // 处理附件增加

            return $model->id;
        }else{
            Yii::error('Tally Record insert error:'.current($model->getFirstErrors()).';post:'.Json::encode($formData,JSON_UNESCAPED_UNICODE));
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

    public static function getList($condition)
    {
        $query = Question::find();

        $count = $query->count();
        $pageSize = $_SERVER['PAGE_SIZE'] ?: 10 ;
        $pagination = new Pagination([
            'defaultPageSize' => $pageSize,
            'totalCount' => $count
        ]);
        $orderByStr = 'id desc';
        $pagination->validatePage=false;
        $list = $query
            ->orderBy($orderByStr)
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        $list = $list ?: [];
        return [
            'data'       => $list,
            'pageInfo'   => [
                'totalCount' => intval($count) ,
                'pageSize'   => intval($pageSize)
            ]
        ];
    }

}